package com.mallcai.bigdata.ladon.service.common.impl;

import com.cake.bigdata.starter.exception.category.BusinessException;
import com.mallcai.bigdata.ladon.dpl.entity.common.CommonInfo;
import com.mallcai.bigdata.ladon.dpl.mapper.common.CommonInfoMapper;
import com.mallcai.bigdata.ladon.dpl.vo.CommonInfoType;
import com.mallcai.bigdata.ladon.service.common.ICommonInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by oneape<oneape15@163.com>
 * Created 2019-07-23 13:59.
 * Modify:
 */
@Slf4j
@Service
public class CommonInfoSerivce implements ICommonInfoService {
    @Autowired
    private CommonInfoMapper commonInfoMapper;

    /**
     * 添加
     *
     * @param ci CommonInfo
     * @return int 1 - 成功; 0 - 失败;
     */
    @Override
    @Transactional
    public int add(CommonInfo ci) {
        int status = commonInfoMapper.hasExist(ci.getClassify(), ci.getCode(), ci.getName(), null);
        if (status > 0) {
            throw new BusinessException("存在相同的编码和名称");
        }
        return commonInfoMapper.insert(ci);
    }

    @Override
    @Transactional
    public int batchAdd(List<CommonInfo> dataList) {
        dataList.forEach(this::add);
        return dataList.size();
    }

    /**
     * 修改
     *
     * @param ci CommonInfo
     * @return int 1 - 成功; 0 - 失败;
     */
    @Override
    public int edit(CommonInfo ci) {
        int status = commonInfoMapper.hasExist(ci.getClassify(), ci.getCode(), ci.getName(), ci.getId());
        if (status > 0) {
            throw new BusinessException("存在相同的编码和名称");
        }
        return commonInfoMapper.update(ci);
    }

    /**
     * 删除
     *
     * @param ci CommonInfo
     * @return int 1 - 成功; 0 - 失败;
     */
    @Override
    public int delete(CommonInfo ci) {
        return commonInfoMapper.delete(ci);
    }

    /**
     * 根据条件查询
     *
     * @param params Map
     * @return List
     */
    @Override
    public List<CommonInfo> getByParams(Map<String, Object> params) {
        if (params == null) {
            params = new HashMap<>();
        }
        return commonInfoMapper.getByParams(params);
    }

    /**
     * 根据Id查询数据源
     *
     * @param id Long 数据id
     * @return Datasource
     */
    @Override
    public CommonInfo getById(Long id) {
        return commonInfoMapper.findById(id);
    }

    /**
     * 根据类型获取
     *
     * @param classify String
     * @return List
     */
    @Override
    public List<CommonInfo> getByClassify(String classify) {
        if (StringUtils.isBlank(classify)) {
            return new ArrayList<>();
        }
        CommonInfoType cit = CommonInfoType.getByCode(classify);
        if (cit == null) {
            return new ArrayList<>();
        }

        return commonInfoMapper.getByClassify(classify);
    }
}
